﻿using System;
using EventMgrLib;
using SmartMotion.Core;
using WeifenLuo.WinFormsUI.Docking;

namespace SmartMotion.AllPages.DockPanels
{
    public partial class FrmDebug : DockContent
    {

        #region 构造和Load

        public FrmDebug()
        {
            InitializeComponent();
            HideOnClose = true;
        }

        private void FrmDebug_Load(object sender, EventArgs e)
        {
            EventMgr.Ins.GetEvent<DebugMessage>().Subscribe(ShowMessage);
        }

        #endregion

        #region 显示调试信息

        private void ShowMessage(string message)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => ShowMessage(message)));
            }
            else
            {
                rtbDebug.AppendText($"{DateTime.Now} {message}\r");
            }
          
        }

        private void RtbDebug_TextChanged(object sender, EventArgs e)
        {
            if (rtbDebug.TextLength > 10000)
            {   
                rtbDebug.Clear();
            }
            rtbDebug.SelectionStart = rtbDebug.Text.Length;
            rtbDebug.ScrollToCaret();
        }

        private void ClearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rtbDebug.Clear();
        }

        #endregion
    }   
}
